const db = require('./config/database')

async function checkIssues () {
  try {
    console.log('🔍 检查数据库问题...\n')

    // 1. 检查表结构
    console.log('📋 检查表结构:')
    const tables = await db.query('SHOW TABLES')
    console.log(
      '现有表:',
      tables.map(t => Object.values(t)[0])
    )

    // 2. 检查novels表结构
    console.log('\n📚 检查novels表结构:')
    const novelColumns = await db.query('DESCRIBE novels')
    console.table(novelColumns)

    // 3. 检查小说数据的状态值
    console.log('\n📊 检查小说状态值:')
    const statusCheck = await db.query('SELECT DISTINCT status FROM novels')
    console.log(
      '现有状态值:',
      statusCheck.map(s => s.status)
    )

    // 4. 检查API在查询什么
    console.log('\n🔍 测试API查询:')
    const apiQuery = `
      SELECT * FROM novels 
      WHERE status != 'deleted'
      ORDER BY updated_at DESC
      LIMIT 3
    `

    try {
      const apiResult = await db.query(apiQuery)
      console.log('API查询成功，返回', apiResult.length, '条记录')
      if (apiResult.length > 0) {
        console.log('示例数据:')
        console.table(
          apiResult.map(n => ({
            id: n.id,
            title: n.title,
            author: n.author,
            status: n.status,
            category: n.category
          }))
        )
      }
    } catch (error) {
      console.error('API查询失败:', error.message)
    }

    // 5. 检查章节数据
    console.log('\n📄 检查章节数据:')
    const chapterCount = await db.query(
      'SELECT COUNT(*) as count FROM chapters'
    )
    console.log('章节总数:', chapterCount[0].count)

    console.log('\n✅ 检查完成')
  } catch (error) {
    console.error('❌ 检查失败:', error.message)
  }
}

checkIssues()
